<%--
  Created by IntelliJ IDEA.
  User: 24506
  Date: 2019/5/30
  Time: 19:40
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <script type="text/javascript">
      var target = "ws://localhost:8080/socket/hongwen"
      var ws;
      function myopen() {
          if ('WebSocket' in window){
              ws = new WebSocket(target)
          } else if ('MozWebSocket' in window){
              ws = new MozWebSocket(target)
          } else {
              alert("error")
              return;
          }

          /*接收服务端发来的消息*/
          ws.onmessage=function (event) {
              document.getElementById("messages").innerText+=event.data
          }
      }

      function onsend() {
          var msg = document.getElementById("msg").value;
          ws.send(msg);
          document.getElementById("msg").value = '';
      }

      function myclose() {
          ws.close();
      }

  </script>
  <body onbeforeunload="myclose()">
      <button onclick="myopen()">link</button> <br><br>
      <button onclick="myclose()">close</button> <br><br>
      <input id="msg" type="msg" placeholder="message"><button onclick="onsend()">send</button>

      <div id="messages">

      </div>

    <br><br>

  </body>
</html>
